﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using Caliburn.Micro;
using DoddleReport;
using DoddleReport.iTextSharp;
using DoddleReport.Writers;
using MdcService;
using MdcService.Static;
using MdcService.Structure;
using SaveFileDialog = Microsoft.Win32.SaveFileDialog;
using Screen = Caliburn.Micro.Screen;
using Timer = System.Threading.Timer;

namespace MdcMain
{
    public class OverAllViewModel:Screen
    {
        public List<MachinesViewModel> MachinesList { get; set; }
      
     
        public ObservableCollection<OverAllDetailModel> MachineDetail { get; set; }
       

        private MachinesViewModel _selectedMachineList;
        public MachinesViewModel SelectedMachinesList
        {
            get { return _selectedMachineList; }
            set
            {
                _selectedMachineList = value;
                
                NotifyOfPropertyChange(nameof(SelectedMachinesList));
            
                

            }
        }


        private Mdc _mdc;
        private Timer _timer;
        private WindowManager _windowManager;
        private MachineValue _currentValue;

        public OverAllViewModel()
        {
            DisplayName = "MDC数据采集软件";
            _windowManager = new WindowManager();
            _mdc = new Mdc();
            
            _mdc.Start();
            _timer = new Timer(TimerCallback,null,0,500);
            MachineDetail = new ObservableCollection<OverAllDetailModel>();
            MachinesList = _mdc.MachineDefinitions.Select(i => new MachinesViewModel(i)).ToList();
            if(MachinesList.Count>0)
            SelectedMachinesList = MachinesList[0];
        }

        public void Pause()
        {
            _mdc.Stop();
        }

        public void Resume()
        {
            _mdc.Start();
        }

        public void Settings()
        {
            var s = new SettingViewModel(_mdc.ConfigurationService);
            _windowManager.ShowWindow(s);
            
        }

        public void Exit()
        {
            _mdc.Stop();
            TryClose();
        }

        public void About()
        {
            new AboutBoxViewModel().Show();
        }

        public void InsertTestMachine()
        {
            var vm = new InsertViewModel(AddMachineType.Testing, _mdc.ConfigurationService);
            _windowManager.ShowDialog(vm);
        }


        public void InsertSinumerikMachine()
        {
            var vm = new InsertViewModel(AddMachineType.Sinumerik, _mdc.ConfigurationService);
            _windowManager.ShowDialog(vm);

        }

        public void InsertSimulateMachine()
        {
            var vm = new InsertViewModel(AddMachineType.Simluate, _mdc.ConfigurationService);
            _windowManager.ShowDialog(vm);

        }

        public void InsertMoxaMachine()
        {
            var vm = new InsertViewModel(AddMachineType.Moxa, _mdc.ConfigurationService);
            _windowManager.ShowDialog(vm);

        }
        public void OpenSettingFolder()
        {
            System.Diagnostics.Process.Start(@".\\Machines");
        }

        public void OpenMainFolder()
        {
            System.Diagnostics.Process.Start(@".");
        }

        public void OpenChartWindow()
        {
            new ChartView(_currentValue).Show();
        }

        public void BuildHtmlReport()
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            dialog.FileName = "报表.Html";
            if (dialog.ShowDialog() == true)
            {
                var report = new Report(MachineDetail.ToReportSource());
                report.TextFields.Title = "MDC 数据报表";
                report.TextFields.SubTitle = "当前机床数据报表";
                report.TextFields.Footer = "版权所有 2017 &copy; 大连华锐重工";
                report.TextFields.Header = string.Format(@"Report Generated: {0}", DateTime.Now);

                using (var x = File.OpenWrite(dialog.FileName))
                {
                    var writer = new HtmlReportWriter();
                    writer.WriteReport(report, x);
                }
            }
            System.Diagnostics.Process.Start(dialog.FileName);

        }

        public void BuildCsvReport()
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            dialog.FileName = "报表.csv";
            if (dialog.ShowDialog() == true)
            {
                var report = new Report(MachineDetail.ToReportSource());
                report.TextFields.Title = "MDC 数据报表";
                report.TextFields.SubTitle = "当前机床数据报表";
                report.TextFields.Footer = "版权所有 2017 &copy; 大连华锐重工";
                report.TextFields.Header = string.Format(@"Report Generated: {0}", DateTime.Now);

                using (var x = File.OpenWrite(dialog.FileName))
                {
                    var writer = new DelimitedTextReportWriter();
                    writer.WriteReport(report, x);
                }
            System.Diagnostics.Process.Start(dialog.FileName);
            }
        }

        public void BuildExcelReport()
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            dialog.FileName = "报表.xls";
            if (dialog.ShowDialog() == true)
            {
                var report = new Report(MachineDetail.ToReportSource());
                report.TextFields.Title = "MDC 数据报表";
                report.TextFields.SubTitle = "当前机床数据报表";
                report.TextFields.Footer = "版权所有 2017 &copy; 大连华锐重工";
                report.TextFields.Header = string.Format(@"Report Generated: {0}", DateTime.Now);

                using (var x = File.OpenWrite(dialog.FileName))
                {
                    var writer = new ExcelReportWriter();
                    writer.WriteReport(report, x);
                }
            System.Diagnostics.Process.Start(dialog.FileName);
            }
        }

        public void BuildPdfReport()
        {
            var dialog = new SaveFileDialog();
            dialog.Filter = "All files (*.*)|*.*";
            dialog.RestoreDirectory = true;
            dialog.FileName = "报表.pdf";
            if (dialog.ShowDialog() == true)
            {
                var report = new Report(MachineDetail.ToReportSource());
                report.TextFields.Title = "MDC 数据报表";
                report.TextFields.SubTitle = "当前机床数据报表";
                report.TextFields.Footer = "版权所有 2017 &copy; 大连华锐重工";
                report.TextFields.Header = string.Format(@"Report Generated: {0}", DateTime.Now);

                using (var x = File.OpenWrite(dialog.FileName))
                {
                    var writer = new PdfReportWriter();
                    writer.WriteReport(report, x);
            System.Diagnostics.Process.Start(dialog.FileName);
                }
            }
        }

        private void TimerCallback(object state)
        {
            if (SelectedMachinesList == null) return;
            var values = _mdc.RealtimeDbService.Select(SelectedMachinesList.Id);
            if (values.Count <= 0) return;
            _currentValue = values[0];
           
            this.OnUIThread(() =>
            {
                MachineDetail.Clear();

                _currentValue.ValueItems.Values.ForEach(i =>
                {
                    MachineDetail.Add(new OverAllDetailModel
                    {
                        变量 = i.Path,
                        实时值 = i.Value,
                        注释 = i.Description
                    });
                });
                MachineDetail.Add(new OverAllDetailModel
                {
                    变量 = "Failures",
                    实时值 = _currentValue.Failures.ToString(),
                    注释 = "值"
                });
                MachineDetail.Add(new OverAllDetailModel
                {
                    变量 = "LastConnect",
                    实时值 = _currentValue.LastConnect.ToLongTimeString(),
                    注释 = "上次读取"
                });
                MachineDetail.Add(new OverAllDetailModel
                {
                    变量 = "Value",
                    实时值 = _currentValue.Id,
                    注释 = "ID"
                });
                MachineDetail.Add(new OverAllDetailModel
                {
                    变量 = "TimeStamp",
                    实时值 = _currentValue.TimeStamp.ToLongTimeString(),
                    注释 = "时间戳"
                });
            });
          

        }

    

    }
}
